<!DOCTYPE html>
<html>
<head>
    <title>用药详情管理</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="description" content="木兰湾管理系统">
    <script type="text/javascript" src="../../static/include/header-css.js"></script>
    <script type="text/javascript">

    </script>
</head>
<body>
<div class="weui-cell__hd" align="center">
    <label id="drugInfo"></label>
    <form class="weui-search-bar__form" id="search-form">
        <input type="text" style="display:none;" name='treatDrugId' id="searchTreatDrugId" value=""/>
        <input type="hidden" name='page' value="1" id="page"/>
    </form>
</div>
<!--列表数据显示-->
<div id="dataContent"></div>
<!--滚动加载-->
<div class="weui-loadmore" id="loadingSpan">
    <span class="weui-loadmore__tips">加载中...</span>
</div>

<!--用药详情记录编辑页面-->
<div id="edit-popup" class='weui-popup__container popup-bottom'>
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:;" class="picker-button close-popup">关闭</a>
                <h1 class="title">用药详情</h1>
            </div>
        </div>
        <div class="modal-content">
            <div class="white_bg">
                <form id="edit-form">
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">用药时间<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id='occurTime' name="occurTime" type="text" value="">
                        </div>
                    </div>
                    <div class="weui-cells__title"><span class='mulanbay_textarea_title'>备注</span></div>
                    <div class="weui-cells weui-cells_form">
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <textarea class="weui-textarea" placeholder="" rows="3" name="remark"></textarea>
                                <div class="weui-textarea-counter"><span>0</span>/200</div>
                            </div>
                        </div>
                    </div>
                    <input type="text" style="display:none;" name='id' id="rrdId" value=""/>
                    <input type="text" style="display:none;" name='treatDrugId' id="fkTreatDrugId" value=""/>
                </form>
            </div>
            <br>
            <br>
            <br>
            <a href="javascript:saveData();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom"><label>保存</label></a>
        </div>
    </div>
</div>


<br>

<a href="javascript:showAdd();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom">新增</a>
<script type="text/javascript" src="../../static/include/footer-js.js"></script>
<script>
    $(function() {
        var treatDrugId = getQueryVariable('treatDrugId');
        if(treatDrugId==null||treatDrugId==''){
            $.toptip('未找到相关药品信息', 'error');
        }
        $('#fkTreatDrugId').val(treatDrugId);
        $('#searchTreatDrugId').val(treatDrugId);
        var url='/treatDrug/get?id='+ treatDrugId;
        doAjax(null,url,'GET',false,function(data){
            setElementInnerHTML('drugInfo',data.name);
        });
        pushRefresh(getData);
        datetimePicker('occurTime',null);
        getData();
        pushStateListener();
    });
    function showAdd() {
        $.openPopup('#edit-popup') ;
        initForm();
        //clearForm("edit-form");
        //不能clear，因为有药品的外键
        var formData = {
            occurTime:getNowDateTimeString()
        };
        $('#edit-form').loadJson(formData);
        setElementInnerHTML('edit-button-name','增加');
    }
    function showEdit(id) {
        $.openPopup('#edit-popup') ;
        //clearForm("edit-form");
        var url='/treatDrugDetail/get?id='+ id;
        doAjax(null,url,'GET',false,function(data){
            $('#edit-form').loadJson(data);
        });
        setElementInnerHTML('edit-button-name','修改');
    }
    function initForm() {
    }
    function saveData() {
        var postData = form2Json("edit-form");
        var create=false;
        var url='/treatDrugDetail/edit';
        if($("#id").val()==null||$("#id").val()==''){
            url='/treatDrugDetail/create';
            create=true;
        }
        doFormSubmit(postData,url,function(data){
            $.closePopup('#edit-popup') ;
            if(create){
                getData();
                gotoTop();
            }else {
                var hh = createRow(data);
                setElementInnerHTML(getDivId(data.id),hh);
            }
        });
    }
    function showDelete(id) {
        var delUrlPrefix = '/treatDrugDetail/delete';
        commonDeleteByIds(delUrlPrefix,id,function () {
            //searchData();
            //直接把该节点删除
            setElementInnerHTML(getDivId(id),'');
        });
    }
    function searchData() {
        setPage(1);
        getData();
    }
    function getData() {
        var url = '/treatDrugDetail/getData';
        loadListData('search-form','dataContent','page',url,createRow,null);
    }
    function getDivId(id) {
        return 'treatDrugDetail'+id;
    }
    function createRow(rowData) {
        var html='';
        html+='<div id="'+getDivId(rowData.id)+'">';
        html+='<div class="weui-form-preview">';
        html+='    <a class="weui-cell  weui-cell_access weui-cell_example" href="javascript:showEdit('+rowData.id+');">\n';
        var pic = '<img src="../../static/image/drug.png">';
        html+='        <div class="weui-cell__hd">'+pic+'</div>\n';
        html+='        <div class="weui-cell__bd">\n';
        var ot = rowData.occurTime;
        var today = isToday(ot);
        if(today==true){
            ot+='<font color="green">(今天)</font>';
        }
        html+='<label class="weui-form-preview__label mulanbay-dark-font">'+ot+'</label>';
        html+='        </div>\n';
        html+='        <div class="weui-cell__ft mulanbay-dark-font">'+getMomentFu(rowData.occurTime)+'</div>\n';
        html+='    </a>\n';
        html+='    <div class="weui-form-preview__bd">\n';
        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">备注</label>\n';
        var rm = rowData.remark == null ? '--':rowData.remark;
        html+='            <span class="weui-form-preview__value">'+rm+'</span>\n';
        html+='        </div>\n';
        html+='    </div>\n';
        html+='    <div class="weui-form-preview__ft">\n';
        html+='        <a class="weui-form-preview__btn weui-form-preview__btn_delete" href="javascript:showDelete('+rowData.id+');">删除</a>\n';
        html+='    </div>\n';
        html+='</div>\n';
        html+='</div>\n';
        html+='<br>\n';
        return html;
    }
</script>
</body>
</html>